class MessageBox extends eui.Component implements  eui.UIComponent {
	/**
	 * messagebox 提示
	 * @msg 提示语
	 * @sureFn 确定回调信息 
	 * @config 配置信息
	 *   @showCancelButton 是否显示取消按钮 true/false
	 * 	 @maskAlpha 是否显示背景遮罩
	 *   @sureBtnText 确定按钮文字
	 *   @cancleBtnText 取消按钮文字
	 * 	 @cancleFn 取消事件
	 * 	 @showCloseButton 是否显示关闭按钮
	 */
	private msg:string;
	private sureFn:Function;
	private config:any;
	public constructor(msg,sureFn,config?:any) {
		super();
		this.msg = msg;
		this.sureFn = sureFn;
		if(config){
			this.config = config;
		}
	}

	private cancleBtn:eui.Button;
	private closeBtn:eui.Button;
	private sureBtn:eui.Button;
	private maskRect:eui.Rect;
	private msgGroup:eui.Group;

	protected partAdded(partName:string,instance:any):void
	{
		super.partAdded(partName,instance);
	}

	private msgLabel:eui.Label;
	protected childrenCreated():void
	{
		super.childrenCreated();
		Utils.setStage(this);
		Utils.resetScale(this.msgGroup);
		//显示动画
		this.msgGroup.verticalCenter = -120 * Utils.scaleV;
		this.msgGroup.scaleX = this.msgGroup.scaleY = Utils.scaleV;
		//this.msgGroup.alpha = 0;
		egret.Tween.get(this.msgGroup, { loop: false }).to({verticalCenter:-60 * Utils.scaleV},400,egret.Ease.backOut);

		this.msgLabel.text = this.msg;
		this.sureBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onSureFn, this);
		this.cancleBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onCancleFn, this);
		this.closeBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onCancleFn, this);
		if(this.config){
			//是否隐藏取消按钮
			if(this.config.showCancelButton){
				this.cancleBtn.visible = false;
				this.sureBtn.right = (this.sureBtn.parent.width-this.sureBtn.width)/2;
			}else{
				this.cancleBtn.visible = true;
				this.sureBtn.right = 0;
			}

			//遮罩透明度
			if(this.config.maskAlpha){
				this.maskRect.alpha = this.config.maskAlpha;
			}

			//确定按钮文字
			if(this.config.sureBtnText){
				this.sureBtn.label = this.config.sureBtnText;
			}

			//取消按钮文字
			if(this.config.cancleBtnText){
				this.cancleBtn.label = this.config.cancleBtnText;
			}
			//显示关闭
			if(this.config.showCloseButton == false){
				this.closeBtn.visible = false;
			}else{
				this.closeBtn.visible = true;
			}
			
		}
	}

	private onCancleFn(){
		this.removeMessage();
		if(this.config&&this.config.cancleFn){
			this.config.cancleFn();
		}
	}
	private onSureFn(){
		if(this.sureFn){
			this.sureFn();
		}
		this.removeMessage();
	}

	/**
	 * 删除自己
	*/
	private removeMessage(){
		egret.Tween.get(this.msgGroup, { loop: false }).to({verticalCenter:0 },400,egret.Ease.backIn).call(()=>{
			egret.Tween.removeTweens(this.msgGroup);
			this.removeChildren();
			if(this.parent){
				this.parent.removeChild(this);
			}
		});
        try{
            this.cancleBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onCancleFn, this);
            this.sureBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onSureFn, this);
			this.closeBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.onCancleFn, this);
        }catch(e){}
	}
}